﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace AcesHigh.GUI
{

    internal delegate void CardClickedEvent(TableDeckPanel panel, PictureCard card);

    internal class TableDeckPanel : DeckPanel
    {
        public event CardClickedEvent CardClicked;

        List<PictureCard> picCards;
      //  List<Card> cards;
        //List<PictureBox> cardImages;

        public TableDeckPanel()
        {
            InitLists();
            AddDragHandlers();
        }

        private void InitLists()
        {
            //cards = new List<Card>(); this.
            //cardImages = new List<PictureBox>();
            picCards = new List<PictureCard>();
        }

        private void AddDragHandlers()
        {
            this.DragEnter += new DragEventHandler(Drag_DragEnter);
            this.DragDrop += new DragEventHandler(Drag_DragDrop);
        }
        
        public PictureCard TopCard
        {
            get
            {
                if (picCards.Count > 0)
                {
                    return picCards[picCards.Count - 1];
                }
                else
                {
                    return null;
                }
            }
        }

        public bool IsEmpty
        {
            get { return (picCards.Count == 0); }
        }
        /*
        public PictureBox TopPictureBox
        {
            get { return this.cardImages[cardImages.Count - 1]; }
        }
            
        
*/
        public void addCard(PictureCard card)
        {
            card.showFaceUpImage();
            
            this.Controls.Add(card);
            card.Location = new Point(0, picCards.Count * 26);
            card.BringToFront();
            card.Focus();
           // cardImages.Add(pb);
            picCards.Add(card);
            updateClickHandler();
        }

        private void updateClickHandler()
        {
            
            int count = picCards.Count;

            if (count == 0) return;

            if (count > 1)
            {
                (picCards[count - 2]).DoubleClick -= cardClicked;
                (picCards[count - 2]).MouseDown -= CardPanel_MouseDown;
                
            }
            picCards[count - 1].DoubleClick += new EventHandler(cardClicked);
            picCards[count - 1].MouseDown += new MouseEventHandler(CardPanel_MouseDown);
        }

        private void cardClicked(object obj, EventArgs args)
        {
            Console.WriteLine(picCards[picCards.Count - 1].ToString() + " clicked");
            if (CardClicked != null)
            {
                CardClicked(this, picCards[picCards.Count - 1]);
            }
           
        }

        public void removeTopCard()
        {
            this.Controls.Remove(picCards[picCards.Count - 1]);
            picCards.RemoveAt(picCards.Count - 1);
            
            //cardImages.RemoveAt(cardImages.Count - 1);
            updateClickHandler();
        }

        public void CardPanel_MouseDown(Object obj, MouseEventArgs args)
        {
            if (args.Clicks == 1)
            {
                PictureBox pictureBox1 = (PictureBox)obj;
                DoDragDrop(this, DragDropEffects.Move);
            }
            else
            {
                cardClicked(obj, args);
            }
        }

        public void Drag_DragEnter(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            Console.WriteLine(picCards.Count);
            if (picCards.Count == 0)
            {
                if(args.Data.GetDataPresent(typeof(TableDeckPanel)))
                {
                   
                    args.Effect = DragDropEffects.Move;
                } else {
                    args.Effect = DragDropEffects.None;
                }
            }
        }

        public void Drag_DragDrop(object obj, DragEventArgs args)
        {
            Console.WriteLine("Drag enter");
         //   if (this == obj) return;
            Console.WriteLine(picCards.Count);
            if (picCards.Count == 0)
            {
                TableDeckPanel cp = (TableDeckPanel)args.Data.GetData(typeof(TableDeckPanel));
 
                PictureCard c = cp.TopCard;
                cp.removeTopCard();
                this.addCard(c);
            }
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
               // if (cards != null) cards = null;
                if (picCards != null) picCards = null;
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // CardPanel
            // 
            this.AutoSize = true;
            this.Size = new System.Drawing.Size(72, 96);
            this.ResumeLayout(false);

        }
    }
}
